<?php

namespace app\common\validate;

use app\common\util\CommonUtil;
use think\Validate;

class House extends Validate
{

    /**
     * 验证规则
     */
    protected $rule = [
        'id' => 'require',
        'house_no' => 'require|uniqueHouseNo',
        'title' => 'require',
        'photos' => 'require',
        'type_id' => 'require',
        'layout_id' => 'require',
        'amount' => 'require|isCurrency',
        'region_id' => 'require',
        'area' => 'require',
        'address_text' => 'require',
        'longitude' => 'require',
        'latitude' => 'require',
        'orientation_id' => 'require',
        'floor' => 'require'
    ];

    /**
     * 提示消息
     */
    protected $message = [
        "id.require" => "缺少参数id",
        "house_no.require" => "缺少参数房屋编号",
        "house_no.uniqueHouseNo" => "房屋编号已存在",
        "title.require" => "请输入标题",
        "photos.require" => "请上传房屋图片",
        "type_id.require" => "请选择租赁方式",
        "layout_id.require" => "请选择房屋户型",
        "amount.require" => "请输入房屋价格",
        "amount.isCurrency" => "请输入正确的房屋价格",
        "region_id.require" => "请选择房屋所在地区",
        "area.require" => "请输入房屋面积",
        "address_text.require" => "请输入详细地址",
        "longitude.require" => "请输入经度",
        "latitude.require" => "请输入纬度",
        "orientation_id.require" => "请输入房屋朝向",
        "floor.require" => "请输入房屋的楼层",
    ];

    /**
     * 字段描述
     */
    protected $field = [
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add' => ['title', 'house_no', 'title', 'photos', 'layout_id', 'type_id', 'amount', 'region_id', 'area', 'address_text', 'longitude', 'latitude', 'orientation_id', 'floor'],
        'update' => ['id', 'title', 'house_no', 'title', 'photos', 'layout_id', 'type_id', 'amount', 'region_id', 'area', 'address_text', 'longitude', 'latitude', 'orientation_id', 'floor'],
    ];


    public function isCurrency($value, $role, $row)
    {
        return CommonUtil::isCurrency($value);
    }

    public function uniqueTitle($value, $rule, $row)
    {
        $model = new \app\common\model\House();
        $query = $model->where("title", $value);
        if (!empty($row['id'])) $query->where("id", "<>", $row['id']);
        $findUser = $query->find();
        if (empty($findUser)) return true;
        return false;
    }

    public function uniqueHouseNo($value, $rule, $row)
    {
        $model = new \app\common\model\House();
        $query = $model->where("house_no", $value);
        if (!empty($row['id'])) $query->where("id", "<>", $row['id']);
        $findUser = $query->find();
        if (empty($findUser)) return true;
        return false;
    }

}
